import qrcode
from PIL import Image,ImageDraw,ImageFont

class QrCode:
    '''
    【红色】：red 【橙色】：orange 【黄色】：yellow 【绿】：green 【 蓝】：blue【紫】：purple
    【灰色】：gray 【白色】：white 【粉红色】：pink 【黑色】：black【墨绿色】：dark green 【橙红色】：orange-red
    '''
    def __init__(self):
        # 二维码保存路径
        self.SevePath = "Pic/QrCode/"
        # 二维码Logo文件
        self.LogoFile = "Pic/Basics/" + "Logo.png"
        # 透明背景图
        self.BjFile = "Pic/Basics/" + "Lexuss.png"
        # 字体路径
        self.TTFFile = "TTF/" + "My.TTF"

        # 初始化生成
        self.qr = qrcode.QRCode(
            version=1,  # 设置容错率为最高
            error_correction=qrcode.ERROR_CORRECT_H,  # 用于控制二维码的错误纠正程度
            box_size=15,  # 控制二维码中每个格子的像素数，默认为10
            border=1,  # 二维码四周留白，包含的格子数，默认为4
            # image_factory=None,  保存在模块根目录的image文件夹下
            # mask_pattern=None
        )

    #
    def InputCreateQrCOde(self):
        # 生成二维码，展示
        Data = input("输入文字或URL：")
        img = qrcode.make(Data)
        # img.save()  # 保存图片至本地目录，可以设定路径
        img.show()

    # 生成普通二维码
    def Create_Qrcode(self, Data = "我是一只小猫咪！", FileName = "FileName1.png"):
        '''
        :param Data: 二维码内容
        :param FileName: 文件名称
        :return: 生成成功或失败
        '''
        try:

            self.qr.clear()
            self.qr.add_data(Data)  # QRCode.add_data(data)函数添加数据
            # qr.make(fit=True)  # QRCode.make(fit=True)函数生成图片
            img = self.qr.make_image()
            # img = img.convert("RGBA")  # 二维码设为彩色
            # img.show()#查看
            # img.save(os.getcwd() + '/' + QrName + '.png', quality=100)#保存获取运行路径，加文件名
            img.save(self.SevePath + FileName, quality=100)

            return True
        except:
            return False

    # 生成带Logo二维码
    def Create_Logo_Qrcode(self, Data = "Hello！",FileName = "FileName2.png"):
        pass
        try:
            self.qr.clear()
            self.qr.add_data(Data)  # QRCode.add_data(data)函数添加数据
            self.qr.make(fit=True)  # QRCode.make(fit=True)函数生成图片
            img = self.qr.make_image()
            img = img.convert("RGBA")  # 二维码设为彩色
            logo = Image.open(self.LogoFile)  # 传gif生成的二维码也是没有动态效果的

            w, h = img.size
            logo_w, logo_h = logo.size
            factor = 4  # 默认logo最大设为图片的四分之一
            s_w = int(w / factor)
            s_h = int(h / factor)
            if logo_w > s_w or logo_h > s_h:
                logo_w = s_w
                logo_h = s_h

            logo = logo.resize((logo_w, logo_h), Image.ANTIALIAS)
            l_w = int((w - logo_w) / 2)
            l_h = int((h - logo_h) / 2)
            logo = logo.convert("RGBA")
            img.paste(logo, (l_w, l_h), logo)
            # img.show()#查看
            # img.save(os.getcwd() + '/' + QrName + '.png', quality=100)#保存获取运行路径，加文件名
            img.save(self.SevePath + FileName, quality=100)
            return True
        except:
            return False

    # 生成带文本二维码
    def Create_Text_Qrcode(self, Text = "Le:", Data = "Hello", FileName = "FileName3.png", LogoBool = True):
        '''

        :param Text: 文本前缀
        :param Data: 二维码内容以及展示内容
        :param FileName: 文件名
        :param LogoBool: 是否添加Logo
        :return:
        '''
        try:
            if LogoBool is True:
                self.Create_Logo_Qrcode(Data = Data, FileName = FileName)
            else:
                self.Create_Qrcode(Data=Data, FileName=FileName)

            oriImg = Image.open(self.BjFile)
            oriImg2 = Image.open(self.SevePath + FileName)
            oriImg2 = oriImg2.resize((430, 430))  # 设置二维码大小
            oriImg.paste(oriImg2, (37, 37))  # 将二维码放在底图上
            draw = ImageDraw.Draw(oriImg)
            font = ImageFont.truetype(self.TTFFile, 15)  # 设置字体
            draw.text((1, 470), Text + Data, (50, 51, 51), font=font)  # 把字添加到图片上
            oriImg = oriImg.convert('RGB')
            oriImg.save(self.SevePath + FileName)
            return True
        except:
            return False

if __name__ == '__main__':
    QrCode().Create_Text_Qrcode(Data="我是一只可爱的小猫咪！")
